<?php

namespace App\Admin\Controllers;

use App\Accounts;
use App\Admin\Filters\TimestampBetween;
use App\RemoteAccounts;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use function Composer\Autoload\includeFile;

class AccountsController extends AdminController
{
    protected $countryArr = ['AD','AE','AF','AG','AI','AL','AM','AO','AR','AT','AU','AZ','BB','BD','BE','BF','BG','BH','BI','BJ','BL','BM','BN','BO','BR','BS','BW','BY','BZ','CA','CF','CG','CH','CK','CL','CM','CN','CO','CR','CS','CU','CY','CZ','DE','DJ','DK','DO','DZ','EC','EE','EG','ES','ET','FI','FJ','FR','GA','GB','GD','GE','GF','GH','GI','GM','GN','GR','GT','GU','GY','HK','HN','HT','HU','ID','IE','IL','IN','IQ','IR','IS','IT','JM','JO','JP','KE','KG','KH','KP','KR','KT','KW','KZ','LA','LB','LC','LI','LK','LR','LS','LT','LU','LV','LY','MA','MC','MD','MG','ML','MM','MN','MO','MS','MT','MU','MV','MW','MX','MY','MZ','NA','NE','NG','NI','NL','NO','NP','NR','NZ','OM','PA','PE','PF','PG','PH','PK','PL','PR','PT','PY','QA','RO','RU','SA','SB','SC','SD','SE','SG','SI','SK','SL','SM','SN','SO','SR','ST','SV','SY','SZ','TD','TG','TH','TJ','TM','TN','TO','TR','TT','TW','TZ','UA','UG','US','UY','UZ','VC','VE','VN','YE','YU','ZA','ZM','ZR','ZW'];

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '账号管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Accounts());
        $grid->model()->orderBy('id','desc');
        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->filter(function($filter) {
            $filter->between('create_time')->dateTime();
        });

        //导出时导出源内容
        $grid->export(function ($export) {
            $export->originalValue(['cookies', 'accessToken','ua']);
        });

        $countryArr = array_combine($this->countryArr,$this->countryArr);

        $grid->filter(function($filter) use ($countryArr){
            $filter->disableIdFilter();
            //$filter->expand();
            $filter->in('country',__('国家'))->multipleSelect($countryArr);
            $filter->equal('appId', __('appId'));
            $filter->equal('bm',__('BM'))->radio([
                ''   => '全部',
                0    => '无',
                1    => '有',
            ]);
            $filter->where(function ($query) {
                switch ($this->input) {
                    //账单
                    case '0':
                        $query->where('amount_spent','>','0')->where('is_prepay_account', '=', '0')->whereIn('account_status',[1,3]);
                        break;
                    //非账单
                    case '1':
                        $query->where('amount_spent', '<=', '0')
                            ->orWhere('is_prepay_account', '=', '1')
                            ->orWhereNotIn('account_status', [1, 3]);
                        break;
                }
            }, '账单', 'bill')->radio([
                '' => '全部',
                '0' => '是',
                '1' => '否',
            ]);
            $filter->where(function ($query) {
                switch ($this->input) {
                    //小于250
                    case '0':
                        // custom complex query if the 'yes' option is selected
                        $query->where('adtrust_dsl_usd','>','0')->where('adtrust_dsl_usd', '<=', '199');
                        break;
                    //251-800
                    case '1':
                        $query->where('adtrust_dsl_usd', '>=', '200')->where('adtrust_dsl_usd','<=','799');
                        break;
                    //大于800
                    case '2':
                        $query->where('adtrust_dsl_usd', '>=', '800')->orWhere('adtrust_dsl_usd','=', '-1');
                        break;
                }
            }, '额度', 'quota')->radio([
                '' => '全部',
                '0' => '0-199',
                '1' => '200-799',
                '2' => '大于800',
            ]);
            $filter->where(function ($query) {
                switch ($this->input) {
                    //无
                    case '0':
                        // custom complex query if the 'yes' option is selected
                        $query->where('page', '=', '0');
                        break;
                    //有
                    case '1':
                        $query->where('page', '!=', '0');
                        break;
                }
            }, 'page', 'page_status')->radio([
                '' => '全部',
                '0' => '无',
                '1' => '有',
            ]);
        });


        $grid->column('id', __('ID'))->sortable();
        $grid->column('appId', __('appId'));
        $grid->column('name', __('name'));
        $grid->column('pass', __('pass'));
        $grid->column('country', __('国家'));
        $grid->column('accessToken')->limit(10);
        $grid->column('cookies')->limit(10);
        $grid->column('ua')->limit(10);
        $grid->column('ip', __('ip'));

        $grid->column('create_time',__('时间'))->sortable();
        $grid->column('account_status',__('状态'))->using(['0'=>'无','1'=>'活','2'=>'死','3'=>'欠费','101'=>'关户','103'=>'准备关户','9'=>'多次欠费']);
        $grid->column('amount_spent',__('adscard'));
        $grid->column('is_prepay_account',__('预付'))->using(['0'=>'否','1'=>'是']);
        $grid->column('bm', __('bm'))->using(['1'=>'有','0'=>'无']);
        $grid->column('adtrust_dsl',__('日额度'));
        $grid->column('adtrust_dsl_usd',__('日额度美元'));
        $grid->column('currency',__('货币'));
        $grid->column('page', __('主页'))->display(function (){
            if ($this->page){
                return '有';
            }
            return '无';
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Accounts::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Accounts);

        $form->display('id', __('ID'));
        $form->display('created_at', __('Created At'));
        $form->display('updated_at', __('Updated At'));

        return $form;
    }

}
